package com.ks.cms.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.cms.controller.dto.SearchCriteriaBase;
import com.ks.cms.dao.BCmsNewsMapper;
import com.ks.cms.entity.BCmsNews;
import com.ks.cms.service.IBCmsNewsAdminService;
import com.ks.core.base.OperationContext;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 信息发布系统的新闻公告信息表 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-21
 */
@Service
public class BCmsNewsAdminServiceImpl extends ServiceImpl<BCmsNewsMapper, BCmsNews> implements IBCmsNewsAdminService {
    @Resource
    private BCmsNewsMapper bCmsNewsMapper;
    @Override
    public Map<String, Object> selectBCmsNewsAdmin(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BCmsNews> wrapper =new EntityWrapper<>();
        BCmsNews bCmsNews  =new BCmsNews();
        for(SearchCriteriaBase.matchField field:fields){
            if("id".equals(field.getName())){
                bCmsNews.setId(Integer.valueOf(field.getValue()));
            }
        }
        bCmsNews.setTenantID(context.getTenantId().intValue());
        bCmsNews.setOrgID(context.getOrgId().intValue());
        bCmsNews.setOrgSubID(context.getOrgSubId().intValue());
        wrapper.setEntity(bCmsNews);
        if(searchCriteriaBase.getStartTime() !=null&& searchCriteriaBase.getEndTime()!=null){
            String filterSql="createTime >'"+searchCriteriaBase.getStartTime()+"' and createTime <'"+searchCriteriaBase.getEndTime()+"'";
            wrapper.addFilter(filterSql);
        }
        RowBounds limit = new Page(searchCriteriaBase.getLimit(),searchCriteriaBase.getPageIndex());
        List<BCmsNews> bCmsNewsList = bCmsNewsMapper.selectPage(limit,wrapper);
        Map<String, Object>  resultMap = this.isResultMap(bCmsNewsList,searchCriteriaBase);
        return resultMap;
    }


    public Map<String, Object>  isResultMap(List list, SearchCriteriaBase searchCriteriaBase){
        Map<String, Object>  map = new HashMap<>();
        map.put("count",list.size());
        map.put("limit",searchCriteriaBase.getLimit());
        map.put("pageIndex",searchCriteriaBase.getPageIndex());
        map.put("hasMore",false);
        map.put("items",list);
        return  map;
    }


}